package org.abc.fund.controller.funds;

import lombok.Data;
import org.abc.fund.dto.FundPortfolioWithFundsDTO;
import org.abc.fund.entity.funds.FundPortfolio;
import org.abc.fund.service.funds.FundPortfolioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/portfolio")
public class FundPortfolioController {

    @Autowired
    private FundPortfolioService fundPortfolioService;

    // ✅ 分页查询我的组合及其基金
    @GetMapping("/page")
    public Page<FundPortfolioWithFundsDTO> getPortfoliosWithFunds(
            @RequestParam Long userId,
            @RequestParam int page,
            @RequestParam int size) {
        return fundPortfolioService.getPortfoliosWithFunds(userId, page, size);
    }

    // ✅ 查询某个用户的所有组合（不分页）
    @GetMapping("/all/{userId}")
    public List<FundPortfolio> getPortfolios(@PathVariable Long userId) {
        return fundPortfolioService.getPortfoliosByUserId(userId);
    }

    // ✅ 创建组合 + 批量添加基金项
    @PostMapping("/save")
    public void savePortfolioWithFunds(@RequestBody SavePortfolioRequest request) {
        fundPortfolioService.savePortfolioWithFunds(request);
    }

    // ✅ 删除整个组合（含基金项）
    @DeleteMapping("/delete/{id}")
    public void deletePortfolio(@PathVariable Long id) {
        fundPortfolioService.deletePortfolio(id);
    }

    // ✅ DTO：保存组合请求结构
    @Data
    public static class SavePortfolioRequest {
        private Long userId;
        private String name;
        private String description;
        private List<String> fundCodes;
    }
}
